package com.maxatime.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maxatime.dao.ExamStorage2Mapper;
import com.maxatime.dao.ExamType2Mapper;
import com.maxatime.pojo.dto.ExamStorageDTO;
import com.maxatime.pojo.entity.ExamStorage;
import com.maxatime.pojo.entity.ExamType;
import com.maxatime.service.Ssbkaoshi5;

@Service
public class SsbkaoshiImpl5 implements Ssbkaoshi5 {

	@Autowired
	ExamStorage2Mapper examStorage2Mapper;
	@Autowired
	ExamType2Mapper examType2Mapper;

	@Override
	public int deleteByPrimaryKey(Integer pkId) {
		// TODO Auto-generated method stub
		examStorage2Mapper.deleteByPrimaryKey(pkId);
		examType2Mapper.deleteByPrimaryKey(pkId);
		return 0;
	}

	@Override
	public int insert(ExamStorage record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	@Transactional
	public int insertSelective(ExamStorage record) {
		// TODO Auto-generated method stub
		ExamStorage examStorage = new ExamStorage();
		BeanUtils.copyProperties(record, examStorage);
		ExamType examType = new ExamType();
//		String TypeId = String.valueOf(SerialNumberUtil.getRandomNum());
		//判断水果类型。如TypeName=水果 So 两个表的TypeId=1 以此类推
		String ab;
		if (examStorage.getTypeName().equals("水果")) {
			ab = "1";
			examStorage.setTypeId(ab);
			examType.setTypeId(ab);
		} else if (examStorage.getTypeName().equals("蔬菜")) {
			ab = "2";
			examStorage.setTypeId(ab);
			examType.setTypeId(ab);
		}else {
			ab = "3";
			examStorage.setTypeId(ab);
			examType.setTypeId(ab);
		}

		Date d = new Date();
		examStorage.setUpdateTime(d);
		examStorage.setCreateTime(d);
		examType.setUpdateTime(d);
		examType.setCreateTime(d);
		examType.setDeleteFlag("0");
		examType.setTypeName(examStorage.getTypeName());
		examStorage2Mapper.insertSelective(examStorage);
		examType2Mapper.insertSelective(examType);
		return 0;
	}

	@Override
	public ExamStorage selectByPrimaryKey(Integer pkId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateByPrimaryKeySelective(ExamStorage record) {
		// TODO Auto-generated method stub
		ExamStorage examStorage = new ExamStorage();
		BeanUtils.copyProperties(record, examStorage);
		Date d = new Date();
		examStorage.setUpdateTime(d);
		ExamType examType = new ExamType();
		examType.setUpdateTime(d);
		//判断水果类型。如TypeName=水果 So 两个表的TypeId=1 以此类推
		System.out.println("!!!:::"+examStorage.getTypeName());
		String ab;
		if (examStorage.getTypeName().equals("水果")) {
			ab = "1";
			examStorage.setTypeId(ab);
			examType.setTypeId(ab);
			examType.setTypeName(examStorage.getTypeName());
		} else if (examStorage.getTypeName().equals("蔬菜")) {
			ab = "2";
			examStorage.setTypeId(ab);
			examType.setTypeId(ab);
			examType.setTypeName(examStorage.getTypeName());
		}else {
			ab = "3";
			examStorage.setTypeId(ab);
			examType.setTypeId(ab);
			examType.setTypeName(examStorage.getTypeName());
		}
//		examType.setTypeName(examStorage.getTypeName());
		
		examStorage2Mapper.updateByPrimaryKeySelective(examStorage);
		examType2Mapper.updateByPrimaryKeySelective(examType);
		return 0;
	}

	@Override
	public int updateByPrimaryKey(ExamStorage record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	@Transactional
	public Map<String, Object> list(ExamStorageDTO examStorageDTO) {
		// TODO Auto-generated method stub
		ExamStorage examStorage = new ExamStorage();
		PageHelper.startPage(examStorageDTO.getPageNum(), examStorageDTO.getPageSize());//分页
		List<ExamStorage> listDemo = examStorage2Mapper.list(examStorage);//ExamStorage全查
		PageInfo<ExamStorage> pageInfo = new PageInfo<ExamStorage>(listDemo);//分页
		System.out.println("集合:::" + listDemo);
		List<ExamStorageDTO> list1 = new ArrayList<ExamStorageDTO>();
		for (int i = 0; i < listDemo.size(); i++) {
			ExamStorageDTO ExamStorageDTO = new ExamStorageDTO();
			BeanUtils.copyProperties(listDemo.get(i), ExamStorageDTO);
			List<ExamType> list = examType2Mapper.listDemo(listDemo.get(i).getTypeId());
			ExamStorageDTO.setTypeName(list.get(0).getTypeName());
			System.err.println("商品类型::" + ExamStorageDTO);
			System.err.println("11111::" + listDemo.get(i).getTypeId());
			System.err.println("11111::" + list.get(0).getTypeName());
			list1.add(ExamStorageDTO);
		}
		System.out.println("结果::" + list1);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("list", list1);
		m.put("pageInfo", pageInfo);
		return m;
	}

	@Override
	public ExamStorage find(int pkId) {
		// TODO Auto-generated method stub
		return examStorage2Mapper.find(pkId);
	}

}
